<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class gametype_match_row extends gametype
{
	var $name = "Match x-in-a-row";
	
	function _xml_to_info(&$xml_class)
	{
		parent::_xml_to_info($xml_class);
		
		$this->card_info['prizes']['probability'] = $xml_class->xml_array['scratchcardexport']['gametype']['info']['probability']['VALUE'];
		$this->card_info['prizes']['money'] = $xml_class->xml_array['scratchcardexport']['gametype']['info']['money']['VALUE'];
		if(!isset($xml_class->xml_array['scratchcardexport']['translate']['info'][0]) && isset($xml_class->xml_array['scratchcardexport']['translate']['info']))
		{
			$xml_class->xml_array['scratchcardexport']['translate']['info'] = array($xml_class->xml_array['scratchcardexport']['translate']['info']);
		}
		foreach($xml_class->xml_array['scratchcardexport']['translate']['info'] as $info)
		{
			$this->card_info['prizes']['rc'][$info['loc']['VALUE']] = array('row'=>$info['row']['VALUE'],'col'=>$info['col']['VALUE']);
			$this->card_info['prizes']['loc'][$info['row']['VALUE']][$info['col']['VALUE']] = $info['loc']['VALUE'];
		}
		
		if(count($this->card_info['prizes']['loc']) != count($this->card_info['prizes']['loc'][0]))
		{
			$this->ipsclass->admin->error("Bad scratchcard");
		}
		
		$this->card_info['prizes']['items'] = array();
	}
	
	function export(&$xml_class)
	{
		$xml_class->xml_add_group('gametype');
		$entry = array();
		$content = array();
		$content[] = $xml_class->xml_build_simple_tag('probability',$this->card_info['prizes']['probability']);
		$content[] = $xml_class->xml_build_simple_tag('money',$this->card_info['prizes']['money']);
		$entry[] = $xml_class->xml_build_entry('info',$content);
		$xml_class->xml_add_group('gametype');
		$entry = array();
		foreach($this->card_info['prizes']['rc'] as $l => $info)
		{
			$content = array();
			$content[] = $xml_class->xml_build_simple_tag('row',$info['row']);
			$content[] = $xml_class->xml_build_simple_tag('col',$info['col']);
			$content[] = $xml_class->xml_build_simple_tag('loc',$l);
			$entry[] = $xml_class->xml_build_entry('info',$content);
		}
		$xml_class->xml_add_entry_to_group('gametype',$entry);
	}
	
	function admin_form()
	{
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Prizes");
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Probability of winning</b>",
															  $this->ipsclass->adskin->form_simple_input('prizes[probability]',$this->card_info['prizes']['probability'])."%"
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Prize money</b>",
															  $this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('prizes[money]', $this->card_info['prizes']['money']).$this->ipsclass->vars['market_suffix']
													 )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array("<b>Prize Items</b>",
															  $this->ipsclass->market_admin->compile_item_list("prizes[items]",$this->card_info['prizes']['items'])
													 )      );
		
		$html .= $this->ipsclass->adskin->end_table();
		
		return $html;
	}
	
	function admin_save(&$card)
	{
		$card['prizes'] = array();
		$card['prizes']['probability'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['prizes']['probability']);
		$card['prizes']['money'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['prizes']['money'],'int');
		$card['prizes']['rc'] = $this->card_info['prizes']['rc'];
		$card['prizes']['loc'] = $this->card_info['prizes']['loc'];
		$card['prizes']['items'] = $this->ipsclass->market_admin->parse_input_item_list(array('prizes','items'));
	}
	
	function is_going_to_win()
	{
		return $this->ipsclass->numbers->compare($this->ipsclass->numbers->random_number(0,100,1),$this->card_info['prizes']['probability']) <= 0 ? 1 : 0;
	}
	
	function create_choices($win = 0)
	{
		$win = $win & 1;
		$choices = array();
		if($win)
		{
			if($this->ipsclass->numbers->random_number(0,1))
			{
				$r = $this->ipsclass->numbers->random_number(0,count($this->card_info['prizes']['loc'])-1);
				$c = 0;
			}
			else
			{
				$r = 0;
				$c = $this->ipsclass->numbers->random_number(0,count($this->card_info['prizes']['loc'])-1);
			}
			if(($r == 0 && $c == 0) || ($r == 0 && $c == count($this->card_info['prizes']['loc'])-1) || ($c == 0 && $r == count($this->card_info['prizes']['loc'])-1))
			{
				$dir = $this->ipsclass->numbers->random_number(1,4);
			}
			else
			{
				$dir = $this->ipsclass->numbers->random_number(1,2);
			}
			if($dir == 1)
			{
				$dr = 1;
				$dc = 0;
			}
			elseif($dir == 2)
			{
				$dr = 0;
				$dc = 1;
			}
			else
			{
				$dr = 1;
				if(($r == count($this->card_info['prizes']['loc'])-1 || $c == count($this->card_info['prizes']['loc'])-1) && ($r != $c))
				{
					$dc = -1;
				}
				else
				{
					$dc = 1;
				}
			}
			$win_key = array_rand($this->card_info['choice_imgs']);
			$win_image = $this->card_info['choice_imgs'][$win_key];
			unset($this->card_info['choice_imgs'][$win_key]);
			
			$this->_generate_win_row($choices,$win_image,$r,$c,$dr,$dc);
			$this->_generate_win_row($choices,$win_image,$r-$dr,$c-$dc,-1 * $dr,-1 * $dc);
		}
		
		$done = $choices;
		
		do
		{
			$choices = $done;
			for($i = 0; $i < count($this->card_info['choice_info']); $i++)
			{
				if(isset($choices[$i]))
				{
					continue;
				}
				$k = array_rand($this->card_info['choice_imgs']);
				$image = $this->card_info['choice_imgs'][$k];
				$choices[$i] = $image;
			}
		}
		while($this->_count_wins($choices) != $win);	
		
		return $choices;
	}
	
	function _generate_win_row(&$choices,$image,$r,$c,$dr,$dc)
	{
		if($r < 0 || $c < 0 || $r >= count($this->card_info['prizes']['loc']) || $c >= count($this->card_info['prizes']['loc']))
		{
			return;
		}
		$choices[$this->card_info['prizes']['loc'][$r][$c]] = $image;
		$this->_generate_win_row($choices,$image,$r+$dr,$c+$dc,$dr,$dc);
	}
	
	function _count_wins($choices)
	{
		$count = 0;
		// Major diagonal
		if($this->_wins_recursive($choices,$choices[$this->card_info['prizes']['loc'][0][0]],0,0,1,1))
		{
			$count++;
		}
		// Minor diagonal
		if($this->_wins_recursive($choices,$choices[$this->card_info['prizes']['loc'][0][count($this->card_info['prizes']['loc'])-1]],0,count($this->card_info['prizes']['loc'])-1,1,-1))
		{
			$count++;
		}
		// Rows
		for($r = 0; $r < count($this->card_info['prizes']['loc']); $r++)
		{
			if($this->_wins_recursive($choices,$choices[$this->card_info['prizes']['loc'][$r][0]],$r,0,0,1))
			{
				$count++;
			}
		}
		// Columns
		for($c = 0; $c < count($this->card_info['prizes']['loc']); $c++)
		{
			if($this->_wins_recursive($choices,$choices[$this->card_info['prizes']['loc'][0][$c]],0,$c,1,0))
			{
				$count++;
			}
		}
		return $count;
	}
	
	function _wins_recursive($choices,$image,$r,$c,$dr,$dc)
	{
		if($r < 0 || $c < 0 || $r >= count($this->card_info['prizes']['loc']) || $c >= count($this->card_info['prizes']['loc']))
		{
			return true;
		}
		return $choices[$this->card_info['prizes']['loc'][$r][$c]] == $image && $this->_wins_recursive($choices,$image,$r+$dr,$c+$dc,$dr,$dc);
	}
	
	function money_reward($win = 0)
	{
		return $win ? $this->card_info['prizes']['money'] : 0;
	}
	
	function items_reward($win = 0)
	{
		return $win ? $this->card_info['prizes']['items'] : array();
	}
}
?>